package com.ktwlrj.securitycenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ktwlrj.core.constant.ResultCode;
import com.ktwlrj.core.exception.ActionException;
import com.ktwlrj.core.util.convert.ConvertUtil;
import com.ktwlrj.securitycenter.controller.args.AddSafeApproveArgs;
import com.ktwlrj.securitycenter.controller.args.SafeApproveArgs;
import com.ktwlrj.securitycenter.dto.SafeApproveDto;
import com.ktwlrj.securitycenter.entity.SafeApproveEntity;
import com.ktwlrj.securitycenter.mapper.SafeApproveMapper;
import com.ktwlrj.securitycenter.service.SafeApproveService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * (FytSafeApprove)业务层实现
 *
 * @author code generation
 * @date 2020-02-24 18:08:14
 * @since JDK1.8
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class SafeApproveServiceImpl extends ServiceImpl<SafeApproveMapper, SafeApproveEntity> implements SafeApproveService {
    private final SafeApproveMapper mapper;


    @Override
    public void save(AddSafeApproveArgs args) throws RuntimeException {
        if (StringUtils.isBlank(args.getBusinessId())) {
            throw new ActionException(ResultCode.VERIFICATION_FAILED.getValue(), "业务id不能为空");
        }
        SafeApproveEntity entity = ConvertUtil.map(args, SafeApproveEntity.class);
        entity.setHandleResult(0);
        boolean result = super.save(entity);
        if (!result) {
            throw new ActionException(ResultCode.DATABASE_INSER_ERROR);
        }
    }

    @Override
    public void approveSave(SafeApproveArgs args) throws RuntimeException {
        if (StringUtils.isBlank(args.getBusinessId())) {
            throw new ActionException(ResultCode.VERIFICATION_FAILED.getValue(), "业务id不能为空");
        }
        SafeApproveEntity entity = ConvertUtil.map(args, SafeApproveEntity.class);
        entity.setHandleTime(new Date());
        boolean result = super.updateById(entity);
        if (!result) {
            throw new ActionException(ResultCode.DATABASE_INSER_ERROR);
        }
    }


    @Override
    public List<SafeApproveDto> selectByBusinessId(String businessId) throws RuntimeException {
        QueryWrapper<SafeApproveEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SafeApproveEntity::getBusinessId, businessId);
        List<SafeApproveEntity> list = mapper.selectList(queryWrapper);
        List<SafeApproveDto> listDto = ConvertUtil.listAtoListB(list, SafeApproveDto.class);
        return listDto;
    }

    @Override
    public void deleteByBusinessId(String businessId) throws RuntimeException {
        QueryWrapper<SafeApproveEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SafeApproveEntity::getBusinessId, businessId);
        mapper.delete(queryWrapper);
    }

    @Override
    public SafeApproveDto getById(String id) throws RuntimeException {
        SafeApproveEntity entity = super.getById(id);
        if (entity == null) {
            throw new ActionException(ResultCode.DATABASE_NODATA);
        }
        SafeApproveDto approveDto = ConvertUtil.map(entity, SafeApproveDto.class);
        return approveDto;
    }
}